﻿@model PagedList.IPagedList<Property4U.Models.Property>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Properties";
}

<div class="row">
    <!-- Page Header -->
    <div class="col-lg-12">
        <h1 class="page-header">@ViewBag.Title</h1>
    </div>
    <!--End Page Header -->
</div>

@if (Request.IsAuthenticated && User.IsInRole("Agent"))
{
<p>
    @Html.ActionLink("Create New", "Create")
</p>
}

@using (Html.BeginForm("Index", "ManageProperties", FormMethod.Get))
{
    <p>
        Find by title: @Html.TextBox("SearchProperty", ViewBag.CurrentFilter as string)
        <input type="submit" class="btn btn-primary btn-xs" style="margin-top:-5px" value="search" />
    </p>
}

<div id="alerts"></div>

<table class="table">
    <tr>
        @if (Request.IsAuthenticated && User.IsInRole("Agent"))
        {
        <th>
            <label>AddressID</label>
        </th>
        }
        else if (Request.IsAuthenticated && User.IsInRole("Member"))
        {
        <th>
            <label>ID</label>
        </th>
         <th>
            <label>Agent Name</label>
        </th>
        <th>
            <label>Address</label>
        </th>
        }
        <th>
            <label>Title</label>
        </th>
        <th>
            <label>Seller</label>
        </th>
        <th>
            <label>Type</label>
        </th>
        <th>
            <label>SubType</label>
        </th>
        <th>
            <label>Number Of Rooms</label>
        </th>
        <th>
            <label>Area Measurement</label>
        </th>
        <th>
            <label>Price</label>
        </th>
        <th>
            @Html.ActionLink("For", "Index", new { sortOrder = ViewBag.ForSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.ActionLink("Bidding", "Index", new { sortOrder = ViewBag.BiddingSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.ActionLink("Availability", "Index", new { sortOrder = ViewBag.AvailabilitySortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        @if (Request.IsAuthenticated && User.IsInRole("Admin"))
        {
            <th>
                <label>Featured</label>
            </th>
        }
        <th>
            <label>Views</label>
            </th>
        <th>
            @Html.ActionLink("Publish On", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
        <tr>
        @if (Request.IsAuthenticated && User.IsInRole("Agent"))
        {
        <td>
            @Html.DisplayFor(modelItem => item.Address.ID)
        </td>
        }
        else if (Request.IsAuthenticated && User.IsInRole("Member")) {
        <td>
            @Html.DisplayFor(modelItem => item.ID)
        </td>
        <td>
            @(item.Agent.FirstName + " " + item.Agent.LastName)
        </td>
        <td>
            @(item.Address.Number + "-" + item.Address.Block + " " + item.Address.Name + ", " + item.Address.Street + " " + item.Address.City + ", " + item.Address.State + " (" + item.Address.ZipCode + ") " + item.Address.Country)
        </td>
        }
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Seller)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.OfType.ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.OfSubType)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NumberOfRooms)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AreaMeasurement) @Html.DisplayFor(modelItem => item.AreaUnits)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.For)
        </td>
        <td>
            <span class="progress status-@item.AllowBidding">
                @Html.DisplayFor(modelItem => item.AllowBidding)
            </span>
        </td>
        <td>
            <span class="progress status-@item.Availability">
                @Html.DisplayFor(modelItem => item.Availability)
            </span>
        </td>
        @if (Request.IsAuthenticated && User.IsInRole("Admin"))
        {
            <td>
                <div id="@item.ID" class="btn-group btn-toggle" style="min-width:65px">
                    @if (item.Featured.ToString().ToUpper().Equals("YES")) {
                        <button class="btn btn-xs btn-default">NO</button>
                        <button class="btn btn-xs btn-primary active">YES</button>
                    }else{
                        <button class="btn btn-xs btn-primary active">NO</button>
                        <button class="btn btn-xs btn-default">YES</button>
                    }
                </div>
            </td>
        }
        <td>
            @Html.DisplayFor(modelItem => item.Views)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.PublishOn)
        </td>
        <td>
            @if (Request.IsAuthenticated && User.IsInRole("Agent"))
            {
                @Html.ActionLink("Edit", "Edit", new { id = item.ID }) @(" | ")
                @Html.ActionLink("Delete", "Delete", new { id = item.ID }) @(" | ")
            }
            @Html.ActionLink("Details", "Details", new { id = item.ID })
        </td>
    </tr>
}

</table>

<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))


@section scripts{
    @if (Request.IsAuthenticated && User.IsInRole("Admin"))
    {
        <script type="text/javascript" src="~/Scripts/Array.IndexOf.js"></script>
        <script type="text/javascript" src="~/Scripts/jquery_cookies.js"></script>
        <script type="text/javascript">

            $('.btn-toggle').click(function () {
                $(this).find('.btn').toggleClass('active');

                if ($(this).find('.btn-primary').size() > 0) {
                    $(this).find('.btn').toggleClass('btn-primary');

                    var tokenKey = 'accessToken';
                    var token = $.cookie(tokenKey);
                    var headers = {};
                    if (token) {
                        headers.Authorization = 'Bearer ' + token;
                    }

                    // Post using jQuery with header
                    $.ajax({
                        url: '/api/Properties/SetFeatured?pid='+$(this).attr('id')+'&featured='+$(this).find('.btn-primary').text(),
                        headers: headers,
                        type: 'post',
                        traditional: true,
                        success: function (data) {
                            if (!$('.alert').hasClass("alert-success")) {
                                addAlert('Successfully set the Property as Featured', 'alert-success');
                                location.reload();
                            }
                        },
                        error: function () {
                            if (!$('.alert').hasClass("alert-danger")) {
                                addAlert('Not Availabile OR Error in setting the Property as Featured', 'alert-danger');
                            }
                        }
                    });
                }

                function addAlert(message, style) {
                    $('#alerts').append(
                        '<div class="alert ' + style + ' alert-dismissible" role="alert">' +
                            '<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>' + message + '</div>'
                    );
                }

                $(this).find('.btn').toggleClass('btn-default');

            });
        </script>
    }
}